<template>
  <Modal class="sale-exportGoodModal" v-model="modal" footer-hide :mask-closable="false" fullscreen :title="title">
    <div class="header-box mb">
      <div class="header-left">
        <Input
            class="start"
            v-model.trim="keyword"
            @on-enter="onSearchClick"
            placeholder="请输入关键词搜索">
        </Input>
        <Button type="primary" icon="ios-search" class="btn-search" @click="onSearchClick"></Button>
      </div>
        <div class="header-right">
          <Button type="primary" @click="onExport">确认导入</Button>
          <Button type="success" @click="onShowBtn('showRight')" v-if="showRight">只显示正确的</Button>
          <Button type="info" @click="onShowBtn('showRight')" v-else>显示全部</Button>
          <Button type="warning" @click="onDelError">删除错误的</Button>
          <Button type="error" @click="onShowBtn('showError')" v-if="showError">只显示错误的</Button>
          <Button type="info" @click="onShowBtn('showError')" v-else>显示全部</Button>
        </div>
    </div>
    <div class="mt">
      <homeTable
        :list="list"
        @getData="getData"
        :translate="-90"
        @onPageSizeChange="onPageSizeChange"
        @onPageChange="onPageChange"
      />
    </div>
  </Modal>
</template>
<script>
import { staticPath, clearEmpty } from '@/util'
import homeTable from "~/homeTable"
export default {
  components: {
    homeTable
	},
  data() {
    return {
      timer: null,
      keyword: '',
      activityId: null,
      type: 1,
      showRight: true,
      showError: true,
      title: '',
      batchNumber: null,
      isError: null,
      list: {
        status: 'pass',
        totalItem: 1,
        pageSize: window.$pageSize,
        currentPage: 1,
        columns: [{
          title: '商品编码',
          key: 'productSn',
          minWidth: 100
        }, {
          title: '商品skuId',
          key: 'skuId',
          minWidth: 150
        }, {
          title: '商品名',
          key: 'productName',
          minWidth: 180
        }, {
          title: '促销价',
          key: 'promotionPrice',
          align: 'left',
          width: 100
        }, {
          title: '积分增加',
          key: 'giftPoint',
          align: 'left',
          width: 80
        }, {
          title: '返利增加',
          key: 'giftRebate',
          align: 'left',
          width: 80
        }, {
          title: '赠送的优惠券ID',
          key: 'giftCouponId',
          width: 150
        }, {
          title: '赠送的优惠券名称',
          key: 'giftCouponName',
          width: 140
        }, {
          title: '赠送的优惠券金额',
          key: 'giftCouponDenomination',
          align: 'left',
          width: 140
        }, {
          title: '促销库存',
          key: 'sellOutQuantity',
          align: 'left',
          width: 80
        }, {
          title: '描述',
          key: 'isError',
          minWidth: 100,
          render: (h, params) => {
            return h('div', {
              class: params.row.isError ? 'font-red' : 'font-blue'
            }, params.row.isError ? params.row.errorMsg : '正确')
          }
        }],
        data: []
      },
      modal: false
    }
  },
  methods: {
    onSearchClick() {
      this.showRight = true
      this.showError = true
      this.list.currentPage = 1
      this.getData()
    },
    openModal(batchNumber, type, id) {
      this.modal = true
      this.activityId = id
      this.timer = null
      this.batchNumber = batchNumber
      this.type = type
      this.title = type === 1 ? '导入非限制品牌商品' : '导入限制品牌商品'
      this.getData()
    },
    getData() {
      let param = {
        current: this.list.currentPage,
        size: this.list.pageSize,
        keyword: this.keyword,
        batchNumber: this.batchNumber,
        isError: this.isError
      }
      param = clearEmpty(param)
      this.list.status = "loading"
      this.$ajaxGet("/factory/import/sku/page", param).then(data => {
        this.list.data = []
        this.list.totalItem = Number(data.total)
        this.list.data = data.records || []
        this.list.status = "pass"
      }).catch(() => {  
        this.list.status = "error"
      })
    },
    onDelError() {
      this.$Modal.confirm({
        title: "确认",
        content: "确认删除错误的？",
        onOk: () => {
          this.list.status = 'loading'
          let param = {
            batchNumber: this.batchNumber
          }
          this.$ajaxPost(`/factory/import/sku/del_error`, param).then(data => {
            this.list.status = 'pass'
            this.$Notice.success({
              title: '错误数据删除成功'
            })
            this.getData()
          }).catch(err => {
            this.list.status = 'pass'
          })
        }
      });
    },
    onExport() {
      this.$Modal.confirm({
        title: "确认",
        content: "删除错误的并导入正确的？",
        onOk: () => {
          this.list.status = 'loading'
          let param = {
            batchNumber: this.batchNumber,
            activityId: this.activityId
          }
          this.$ajaxPost(`/factory/import/sku/del_error`, param).then(data => {
            this.$ajaxPost(`/activity/factory_info/batch_import_product`, param).then(res => {
              if (res === '无正确数据导入') {
                this.$Notice.warning({
                  title:'温馨提示',
                  desc: '无正确数据导入'
                })
                setTimeout(() => {
                  this.modal = false
                }, 300)
              } else {
                this.comeAgain(res)
              }
            }).catch(err => {
              this.list.status = 'pass'
            })
          }).catch(txt => {
            this.list.status = 'pass'
          })
        }
      });
    },
    comeAgain(batchSaveKey) { // 轮询
      let param = {
        batchSaveKey: batchSaveKey
      }
      this.timer && clearTimeout(this.timer)
      this.$ajaxGet('/activity/factory_info/find_batchSaveKey', param).then(data => {
        if (data) {
          this.list.status = 'pass'
          let text = ''
          if (Array.isArray(data) && data.length) {
            for (let item of data) {
              text = text + item.skuSn + "、"
            }
            text = text + '选择的商品SKU有重复、不存在或者与其他活动有冲突, 部分商品会加入成功'
          } else {
            this.$Notice.success({
              title: '导入成功'
            })
          }
          if (text) alert(text)
          this.modal = false
          this.getSkuPage({size: window.$pageSize, current: 1, self: this})
        } else {
          this.timer = setTimeout(() => {
            this.comeAgain(batchSaveKey)
          }, 1000)
        }
      })
    },
    onShowBtn(btnName) {
      if (this[btnName]) {
        this.isError = btnName === 'showRight' ? false : true
      } else {
        this.isError = null
      }
      this.getData()
      this[btnName] = !this[btnName]
    },
    getSkuPage(param) {
      param.self = this
      this.status = 'loading'
      param.isLimitBrand = this.type == 1 ? false : true
      param.activityId = this.activityId
      this.$store.dispatch('getSkuPage', param).then(data => {
        this.$store.dispatch("getSkuCount", {id: this.activityId, isLimitBrand: this.type == 1 ? false : true})
      })
    },
    onPageChange(number) {
			this.list.currentPage = number
      this.getData()
		},
    onPageSizeChange(pageSize) {
			this.list.currentPage = 1
			this.list.pageSize = pageSize
			this.getData()
		}
  }
}
</script>

